package com.wgts.wgts.netmodle;

import ren.yale.android.retrofitcachelib.transformer.CacheTransformer;
import rx.Observable;
import rx.Scheduler;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/**
 */

public class TransformUtils {

    private static Throwable mRefreshTokenError = null;

    public static <T> Observable.Transformer<T,T> defaultSchedulers(){
        return new Observable.Transformer<T, T>() {
            @Override
            public Observable<T> call(Observable<T> tObservable) {
                return tObservable.observeOn(AndroidSchedulers.mainThread())
                        .subscribeOn(Schedulers.io());
            }
        };
    }


    /**
     * 工作线程
     * @param <T>
     * @return
     */
    public static <T> Observable.Transformer<T,T> all_io(){
        return new Observable.Transformer<T, T>() {
            @Override
            public Observable<T> call(Observable<T> tObservable) {
                return tObservable.observeOn(Schedulers.io()).subscribeOn(Schedulers.io());
            }
        };
    }

    /**
     * 可自定义线程
     */
    public static <T> Observable.Transformer<T, T> rxSchedulerHelper(final Scheduler scheduler) {
        return new Observable.Transformer<T, T>() {
            @Override
            public Observable<T> call(Observable<T> tObservable) {
                return tObservable.subscribeOn(scheduler)
                        .unsubscribeOn(AndroidSchedulers.mainThread())
                        .observeOn(AndroidSchedulers.mainThread());
            }
        };
    }

    /**
     * 主线程回调
     * @param <T>
     * @return
     */
    public static <T> Observable.Transformer<T,T> all_Main(){
        return new Observable.Transformer<T, T>() {
            @Override
            public Observable<T> call(Observable<T> tObservable) {
                return tObservable.observeOn(AndroidSchedulers.mainThread()).subscribeOn(AndroidSchedulers.mainThread());
            }
        };
    }


    /**
     * Refresh the token when the current token is invalid.
     *
     * @return Observable
     */
    public static <T> Observable.Transformer<T,T> newThread(){
        return new Observable.Transformer<T, T>() {
            @Override
            public Observable<T> call(Observable<T> tObservable) {
                return tObservable.observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.newThread());
            }
        };
    }


    public static <T> Observable.Transformer<T, T> cacheSchedulers() {

        return new Observable.Transformer<T, T>() {
            @Override
            public Observable<T> call(Observable<T> tObservable) {

                return tObservable.compose(CacheTransformer.<T>emptyTransformer())
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread()).map(new Func1<T, T>() {
                            @Override
                            public Object call(Object t) {

                                return t;
                            }
                        });
            }
        };
    }
}
